﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SweetSurvey.Controllers;
using SweetSurvey.Models;
using SweetSurvey.Models.Repositories;
using SweetSurvey.Models.ViewModels;
using System.Web.Script.Serialization;

namespace SweetSurvey.Tests.ControllerTests
{
    
    
    /// <summary>
    ///This is a test class for QuestionControllerTest and is intended
    ///to contain all QuestionControllerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class QuestionControllerTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetSampleQuestions
        ///</summary>
        [TestMethod()]
        public void GetSampleQuestionsTest()
        {
            // variables
            var count = 10;

            // prepare
            var mockRepository = new Mock<IQuestionRepository>(MockBehavior.Strict);
            mockRepository.Setup(r => r.GetSampleQuestions()).Returns(() =>
            {
                var list = new List<Question>();
                for (var i = 0; i < count; i++) { list.Add(new Question()); }
                return list.AsQueryable<Question>();
            });
            var controller = new QuestionController(mockRepository.Object);

            // act
            var result = controller.GetSampleQuestions();

            // assert
            mockRepository.VerifyAll();
            Assert.IsInstanceOfType(result, typeof(JsonResult));
            Assert.IsInstanceOfType(((JsonResult)result).Data, typeof(QuestionsViewModel));
            Assert.AreEqual<int>(((QuestionsViewModel)(((JsonResult)result).Data)).Count, count);
        }

        /// <summary>
        ///A test for GetSampleQuestions
        ///</summary>
        [TestMethod()]
        public void GetQuestionsTest()
        {
            // variables
            var count = 10;
            var surveyId = "fakeId";

            // prepare
            var mockRepository = new Mock<IQuestionRepository>(MockBehavior.Strict);
            mockRepository.Setup(r => r.GetQuestionsBySurveyId(surveyId)).Returns(() =>
            {
                var list = new List<Question>();
                for (var i = 0; i < count; i++) { list.Add(new Question()); }
                return list.AsQueryable<Question>();
            });
            var controller = new QuestionController(mockRepository.Object);

            // act
            var result = controller.GetQuestions(surveyId);

            // assert
            mockRepository.VerifyAll();
            Assert.IsInstanceOfType(result, typeof(JsonResult));
            Assert.IsInstanceOfType(((JsonResult)result).Data, typeof(QuestionsViewModel));
            Assert.AreEqual<int>(((QuestionsViewModel)(((JsonResult)result).Data)).Count, count);
        }

        /// <summary>
        ///A test for Create
        ///</summary>
        [TestMethod]
        public void CreateTest()
        {
            // variables
            var questionId = "fakeId";
            var question = new QuestionViewModel() { QuestionId = questionId };
            
            // prepare
            var serialzier = new JavaScriptSerializer();
            var controller = new QuestionController(null);
            
            // act
            var result = controller.Create(serialzier.Serialize(question));

            // assert
            Assert.IsInstanceOfType(result, typeof(JsonResult));
            Assert.IsInstanceOfType(((JsonResult)result).Data, typeof(QuestionViewModel));
            Assert.AreNotEqual<string>(questionId, ((QuestionViewModel)(((JsonResult)result).Data)).QuestionId);
        }
    }
}
